package com.hzw.code.common.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 字符串处理工具类
 *
 * @author zhouxuanhong
 * @date 2019-04-01 16:54
 * @since JDK1.8
 */
@Slf4j
public class CustomStringUtils {

    /**
     * 只是数字
     */
    public static final Integer ONLY_NUM = 1;
    /**
     * 只是数字+字母
     */
    private static final Integer ONLY_NUM_LETTER = 2;
    /**
     * 数字+字母+字符
     */
    private static final Integer NUM_LETTER_CHAR = 3;
    /**
     * 下划线
     */
    private static final String UNDER_LINE = "_";


    /**
     * 获取uuid
     *
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-04-01 17:02
     */
    public static String getUuId() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replace("-", "");
    }

    /**
     * 获取字符串随机数
     *
     * @param length ${@link String} 随机数长度
     * @param type   ${@link Integer} 类型，默认数字
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-04-01 17:02
     */
    public static String getRandomString(int length, Integer type) {
        StringBuilder buffer = new StringBuilder();
        if (ONLY_NUM_LETTER.equals(type)) {

        } else if (NUM_LETTER_CHAR.equals(type)) {

        } else {
            Random random = new Random(System.currentTimeMillis());
            for (int i = 0; i < length; i++) {
                buffer.append(random.nextInt(10));
            }
        }
        return buffer.toString();
    }

    /**
     * 驼峰命名转下划线
     *
     * @param str ${@link String}
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-08-01 13:54
     */
    public static String humpToUnderline(String str) {
        if (StringUtils.isNoneBlank(str)) {
            StringBuilder sb = new StringBuilder(str);
            int temp = 0;
            if (!str.contains(UNDER_LINE)) {
                for (int i = 0; i < str.length(); i++) {
                    if (Character.isUpperCase(str.charAt(i))) {
                        sb.insert(i + temp, UNDER_LINE);
                        temp += 1;
                    }
                }
            }
            return sb.toString().toLowerCase();
        }
        return null;
    }

    /**
     * 下划线转驼峰
     *
     * @param str ${@link String}
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-08-01 13:58
     */
    public static String underlineToHump(String str) {
        StringBuilder result = new StringBuilder();
        String a[] = str.split(UNDER_LINE);
        for (String s : a) {
            if (!str.contains(UNDER_LINE)) {
                result.append(s);
                continue;
            }
            if (result.length() == 0) {
                result.append(s.toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }

    /**
     * bean转字符串并进行排序操作(升序)，同时返回排序后的字符串
     *
     * @param object ${@link Object}
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-09-24 19:14
     */
    public static String beanToStringAndSort(Object object) {
        String jsonString = JSONObject.toJSONString(object);
        if (StringUtils.isNotBlank(jsonString)) {
            String replace = jsonString.replace("\\", "")
                    .replace("\"", "")
                    .replace(" ", "");
            char[] chars = replace.toCharArray();
            Arrays.sort(chars);
            String collect = Stream.of(chars).map(v -> String.valueOf(v)).collect(Collectors.joining());
            log.debug("----------KtwlStringUtils--排序后数据-------->beanToStringAndSort:{}", collect);
            return collect;
        }
        return "";
    }


    /**
     * bean排序后转字符串加时间戳进行md5
     *
     * @param object ${@link Object}
     * @return String[] ${@link String[]} [0]-timestamp,[1]-md5
     * @author zhouxuanhong
     * @date 2019-09-29 08:34
     */
    public static String[] beanToSortTimestampMd5(Object object) {
        String[] result = new String[2];
        String timestamp = String.valueOf(System.currentTimeMillis());
        result[0] = timestamp;
        String md5 = DigestUtils.md5Hex(beanToStringAndSort(object) + timestamp);
        result[1] = md5;
        log.debug("----------KtwlStringUtils-----result[0]----->beanToSortTimestampMd5:{}", result[0]);
        log.debug("----------KtwlStringUtils-----result[1]----->beanToSortTimestampMd5:{}", result[1]);
        return result;
    }
}
